/*
 * Login.cpp
 *
 *  Created on: 10/06/2011
 *      Author: dela
 */

#include <iostream>
#include "Login.h"
#include "../Connection.h"
#include "Home.h"

using namespace std;

/**
 *
 */
Login::Login(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) :
	Gtk::Window(cobject), builder(builder) {

	Gtk::Button *button = 0;
	builder->get_widget("button1", button);
	button->signal_clicked().connect(
			sigc::mem_fun(*this, &Login::onButtonClick));
}

/**
 *
 */
void Login::onButtonClick() {
	bool login = false;
	string user;
	string password;
	Gtk::Entry *userInput;
	builder->get_widget("entry1", userInput);
	user = userInput->get_text();
	Gtk::Entry *passInput;
	builder->get_widget("entry2", passInput);
	password = passInput->get_text();
	login = Connection::getInstance()->login(user, password);
	if (!login) {
		Gtk::MessageDialog dialog(*this, "Error en login");
		dialog.set_secondary_text(
				"Usuario o contraseña incorrectos.");
		dialog.run();
	} else {
		this->hide();
		string path;
		if (Connection::getInstance()->getType() == 'I')
			path = "glade/instructor/home.glade";
		else
			path = "glade/student/home.glade";
		Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(path);
		Home* home = 0;
		builder->get_widget_derived("window1", home);
		Gtk::Main::instance()->run(*home);
	}
}
